import json
import os
import utils
import datetime
import collect
from openpyxl.utils import get_column_letter


# 设置单元格宽度
def set_columns_width(sheet, columns_width):
    for index, width in enumerate(columns_width):
        sheet.column_dimensions[get_column_letter(index + 1)].width = width


def test():
    f = open("fans_number.json", 'r', encoding='utf-8')
    dest_filename = '{0}男女粉丝数表格.xlsx'.format(datetime.date.today())
    workbook, sheet = utils.make_workbook()
    set_columns_width(sheet, [20, 20, 16, 16, 16])
    sheet.append(['账号', 'gh_id', '男', '女', '未知'])
    for line in f.readlines():
        line = json.loads(line)
        if line['unknown'] and line["nan"] and line["nv"]:
            if line['unknown'] != "一键登录异常":
                dic = [line['nickname'], line['gh_id'], int(line["nan"]), int(line["nv"]), int(line["unknown"])]
            else:
                dic = [line['nickname'], line['gh_id'], int(line["nan"]), int(line["nv"]), line["unknown"]]
        else:
            if line["nan"] and line["nv"]:
                dic = [line['nickname'], line['gh_id'], int(line["nan"]), int(line["nv"]), line["unknown"]]
            else:
                dic = [line['nickname'], line['gh_id'], line["nan"], line["nv"], line["unknown"]]
        sheet.append(dic)
    workbook.save(filename=dest_filename)
    f.close()
    os.remove('fans_number.json')

if __name__ == '__main__':
    collect.test_collect(None)
    test()